﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Majoopic.Utility.Image
{

    public abstract class BaseImageReader : IHeaderReader, IDisposable
    {
        protected Stream ImageStream
        {
            get;
            private set;
        }

        private static readonly Regex nullDateTimeMatcher = new Regex(@"^[\s0]{4}[:\s][\s0]{2}[:\s][\s0]{5}[:\s][\s0]{2}[:\s][\s0]{2}$");

        private bool streamCreated;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fileName"></param>
        public BaseImageReader(string fileName)
            : this(new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            this.streamCreated = true;
        }
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="imageStream"></param>
        public BaseImageReader(Stream imageStream)
        {
            if (imageStream == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (imageStream.CanSeek == false)
            {
                throw new ArgumentException("The stream reader must be seekable.", "reader");
            }
            this.ImageStream = imageStream;
            this.streamCreated = false;
        }
        
        public abstract void FillImageHeaderInformation(ImageHeaderInformation headerInformation);

        /// <summary>
        /// Dispose the streal if the class have created it
        /// </summary>
        public virtual void Dispose()
        {
            if (this.streamCreated && this.ImageStream != null)
            {
                this.ImageStream.Close();
                this.ImageStream.Dispose();
                this.ImageStream = null;
            }
        }

        #region Private Methods
        protected Rational ConvertToRational(object value)
        {
            if (value != null && value is ulong)
            {
                return new Rational(value);
            }
            return null;
        }

        protected short? ConvertToShort(object value)
        {
            if (value != null && value is ushort)
            {
                return Convert.ToInt16(value);
            }
            return null;
        }

        protected string ConvertToString()
        {
            return null;
        }

        protected DateTime? ConvertToDateTime(string strDate)
        {
            if (string.IsNullOrEmpty(strDate) || nullDateTimeMatcher.IsMatch(strDate))
            {
                return null;
            }

            DateTime dateConverted;
            string dateFormat = null;

            // There are 2 types of date - full date/time stamps, and plain dates. Dates are 10 characters long.
            if (strDate.Length == 10)
            {
                dateFormat = Constants.ShortDateFormat;
            }
            else
            {
                dateFormat = Constants.LongDateFormat;
            }
            if (DateTime.TryParseExact(strDate, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateConverted))
            {
                return dateConverted;
            }

            return null;
        }
        #endregion        
    }
}
